package group2assignone.view.customer;

import group2assignone.model.Flight;
import group2assignone.ucm.UCLogout;
import group2assignone.func.UCCtoALAdapter;
import group2assignone.func.MyCheckBox;
import group2assignone.model.Customer;
import group2assignone.ucm.customer.*;
import group2assignone.view.OurView;
import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.EtchedBorder;

/**
 * View class for displaying available flights
 * @author group2
 */
public class AvailableFlightsView extends OurView {

    private JLabel custLab;
    private JButton logout = new JButton("Logout");
    private JButton bookFlight = new JButton("Book Flight");
    private JButton cancel = new JButton("Cancel");
    private JLabel flights;
    private Customer customer;
    private Flight selectedFlight;
    private CheckboxGroup cbg = new CheckboxGroup();
    private ArrayList<Flight> aFlights;

    /**
     * Customer used for personalisation. If customer is not logged in
     * then username is 'Guest'
     * @param cust
     */
    public AvailableFlightsView(Customer cust) {
        customer = cust;
    }

    /**
     * Function to add the GUI components to the frame.
     */
    @Override
    public void addComponents() {

        setTitle("Available Flights");
        custLab = new JLabel("Welcome " + customer.getUsername());

        northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.LINE_AXIS));
        custLab.setAlignmentX(JLabel.LEFT);
        logout.setAlignmentX(JButton.RIGHT);
        northPanel.add(custLab);
        northPanel.add(Box.createHorizontalGlue());
        if (!customer.getUsername().equals("Guest")) {
            northPanel.add(logout);
        }
        northPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));

        southPanel.add(cancel);

        eastPanel.setBorder(BorderFactory.createEmptyBorder(20, 10, 20, 20));
        westPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 10));
        northPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
        southPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

        logout.addActionListener(new UCCtoALAdapter(new UCLogout(this, customer)));
        bookFlight.addActionListener(new UCCtoALAdapter(new UCChangeUIBookFlight(customer, this)));
        if (customer.getUsername().equals("Guest")) {
            cancel.addActionListener(new UCCtoALAdapter(new UCCancelGuest(this)));
        } else {
            cancel.addActionListener(new UCCtoALAdapter(new UCCancelCustomer(customer, this)));
        }
    }

    /**
     * displays a list of flights provided in the middle panel
     * displays as check box options so only one can be selected
     * @param availFlights
     */
    public void displayFlights(ArrayList<Flight> availFlights) {
        if (availFlights == null) {
            centerPanel.add(new JLabel("Sorry, none available with those details"));
        } else {
            int size = availFlights.size();
            aFlights = availFlights;
            centerPanel.setLayout(new GridLayout(size + 1, 4, 10, 5));
            centerPanel.add(new JLabel("Select"));
            centerPanel.add(new JLabel("Flight Number"));
            centerPanel.add(new JLabel("Departure Time"));
            centerPanel.add(new JLabel("Price"));
            centerPanel.add(new JLabel("Remaining Seats"));
            for (int i = 0; i < size; i++) {
                centerPanel.add(new MyCheckBox("", true, cbg, i));
                centerPanel.add(new Label(availFlights.get(i).getFlightNum()));
                centerPanel.add(new Label(availFlights.get(i).getDepTime()));
                centerPanel.add(new Label(availFlights.get(i).getCost()));
                centerPanel.add(new Label(Integer.toString(availFlights.get(i).getRemSeats())));
            }

            southPanel.add(bookFlight);
        }
        setSize(1000,300);
    }

    /**
     *
     * @return the flight currently selected in the check box group
     */
    public Flight getSelectedFlight() {
        MyCheckBox mcb = (MyCheckBox) cbg.getSelectedCheckbox();
        int i = mcb.getIndex();
        return aFlights.get(i);
    }
}
